package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//持久层
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate; //操作数据库

    @Override
    public int insert(User user) {
        String sql = "INSERT INTO `t_user` (`id`, `username`, `birthday`, `sex`, `address`) VALUES (null, ?, ?, ?, ?)";
        Object[] params = {user.getUsername(), user.getBirthday(), user.getSex(), user.getHome_address()};
        int count = jdbcTemplate.update(sql, params);
        return count;
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE `t_user` SET `username`=?, `birthday`=?, `sex`=?, `address`=? WHERE (`id`=?)";
        Object[] params = {user.getUsername(), user.getBirthday(), user.getSex(), user.getHome_address(), user.getId()};
        int count = jdbcTemplate.update(sql, params);
        return count;
    }

    @Override
    public int delete(Integer id) {
        String sql = "delete from t_user where id = ?";
        int count = jdbcTemplate.update(sql, id);
        return count;
    }

    @Override
    public List<User> findAll() {
        String sql = "select * from t_user";
        //如果属性名和列名一致 使用 BeanPropertyRowMapper 即可
        //List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        //如果属性名和列名不一致  需要自定义映射关系
        List<User> userList = jdbcTemplate.query(sql, new RowMapper<User>() {
            /**
             * 手动映射
             * @param rs 结果集对象
             * @param rowNum 行号， 一般不用
             * @return
             * @throws SQLException
             */
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                //resultSet.getInt .getString .getDate .getObject
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String sex = rs.getString("sex");
                Date birthday = rs.getDate("birthday");
                String home_address = rs.getString("address");

                User user = new User();
                user.setId(id);
                user.setUsername(username);
                user.setSex(sex);
                user.setBirthday(birthday);
                user.setHome_address(home_address);

                return user;
            }
        });
        return userList;
    }

    @Override
    public User findById(Integer id) {
        String sql = "select * from t_user where id = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
        return user;
    }
}
